//
//  YH_WebStyleViewController.m
//  YinHang
//
//  Created by 123 on 14-6-18.
//  Copyright (c) 2014年 孙彦鹏. All rights reserved.
//

#import "YH_WebStyleViewController.h"
#import "YH_HomeViewController.h"

@interface YH_WebStyleViewController ()
{
    UIImage *bg_img;
    UIImage *bg_selectedImg;
}
@end

@implementation YH_WebStyleViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImage *input_bgImg = [UIImage imageNamed:@"webstyle_inputbox"];
    _input_bgImgView.image = [input_bgImg stretchableImageWithLeftCapWidth:input_bgImg.size.width*0.5f topCapHeight:input_bgImg.size.height*0.5f];
    
    // 2.假数据
    fakeColorArr = [NSMutableArray array];
    for (int i = 0; i<24; i++) {
        // 添加随机色
        [fakeColorArr addObject:[UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]];
    }
    
    bg_img = [UIImage imageNamed:@"webstyle_img_bg"];
    bg_selectedImg = [UIImage imageNamed:@"webstyle_imgselected_bg"];
    for (int i = 0; i < fakeColorArr.count/6; i++) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(_scrollView.bounds.size.width*i, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
        
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 2; k++) {
                UIImageView *webStyleImgView = [[UIImageView alloc]initWithFrame:CGRectMake(30+(100+50)*k, 30+(60+50)*k, 100, 60)];
                webStyleImgView.backgroundColor = fakeColorArr[i];
                UIImageView *webStyle_bgImgView = [[UIImageView alloc]initWithFrame:CGRectMake(30+(100+50)*k, 30+(60+50)*k, 100, 60)];
                webStyle_bgImgView.image = bg_img;
                [view addSubview:webStyleImgView];
                [view addSubview:webStyle_bgImgView];
            }
        }
        [_scrollView addSubview:view];
        
        _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width*(i+1), _scrollView.bounds.size.height);
    }
    
    //默认选中第一行
    [self performSelector:@selector(selectFirstRow) withObject:nil afterDelay:0];
}

// 自定义BarButtonItem
- (void)customBarButtonItem {
    
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
    [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = - 10;
    if (ios7) {
        self.navigationItem.leftBarButtonItems = @[negativeSpacer,leftBarButtonItem];
    }else{
        self.navigationItem.leftBarButtonItems = @[leftBarButtonItem];
    }
    
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
    NSAttributedString *attrTitle = [[NSAttributedString alloc]initWithString:@"提交" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:19.0]}];
    [rightBtn setAttributedTitle:attrTitle forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(enterHomeViewController) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}


- (void)selectFirstRow
{
	
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 提交
- (void)enterHomeViewController {
    
    if ([NSString emptyValidate:[_webNameTxtField.text trim]]) {
        [SVProgressHUD showErrorWithStatus:@"网站名称不能为空！"];
    } else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确认提交？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"提交", nil];
        [alert show];
    }
}

#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {//确认提交
        
        YH_HomeViewController *homeVC = [[YH_HomeViewController alloc]initWithNibName:@"YH_HomeViewController" bundle:nil];
        [self.navigationController pushViewController:homeVC animated:YES];
    }
}


// 返回
- (void)back {
    [self.navigationController popToRootViewControllerAnimated:YES];
}


@end
